﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Data.Objects;
using System.Linq.Expressions;
using System.Data.Objects.DataClasses;

namespace ESN.Models
{
    public class App
    {
        [Key]
        public int AppId { get; set; }

        public string AppKey { get; set; }

        public string AppName { get; set; }

        [DataType(DataType.Url)]
        public string AppUrl { get; set; }

        public bool AppFrame { get; set; }

        public bool AppPublic { get; set; }

    }

    public class LiveFolder
    {
        [Key]
        public int FId { get; set; }

        [Required]
        public string FName { get; set; }

        [Required]
        public string FKey { get; set; }

        public bool isPublic { get; set; }

        public string FThumb { get; set; }

        public Guid UserId { get; set; }
        public virtual CustomProfile Profiles { get; set; }

        public ICollection<Attachment> Attachments { get; set; }

        public LiveFolder()
        {
            FThumb = "/Images/Txt.png";
        }
    }

    public class CustomProfile
    {
        [Key]
        public Guid UserId { get; set; }

        [Required]
        public string UserName { get; set; }

        [Required]
        public string DisplayName { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Location { get; set; }

        public string Additional { get; set; }

        [DataType(DataType.ImageUrl)]
        public string Avatar { get; set; }

        public bool Gender { get; set; }

        public bool isPrivate { get; set; }

        [RegularExpression("[0-3][0-3][0-1]")]
        public string WallPermission { get; set; }
        /* Permission go with number here
         * 0 - None
         * 1 - Read
         * 2 - Write
         * 3 - Read/Write
         */

        public virtual ICollection<CustomProfile> Friends { get; set; }
        public virtual ICollection<FriendRequest> FriendRequest { get; set; }
        public virtual ICollection<UserNotice> UserNotices { get; set; }
        public virtual ICollection<UserWall> UserWalls { get; set; }

        public virtual ICollection<Group> Groups { get; set; }
        public virtual ICollection<GroupWall> GroupWalls { get; set; }

        public virtual ICollection<Attachment> Attachments { get; set; }

        public CustomProfile()
        {
            Friends = new HashSet<CustomProfile>();
            Groups = new HashSet<Group>();
            WallPermission = "310";
            isPrivate = false;
        }
    }

    public class FriendRequest
    {
        [Key]
        public int FRId { get; set; }

        public string FRHi { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime? FRTime { get; set; }


        public Guid UserId { get; set; }
        [ForeignKey("UserId")]
        public virtual CustomProfile Profiles { get; set; }

        public Guid FriendId { get; set; }
        [ForeignKey("FriendId")]
        public virtual CustomProfile Friend { get; set; }
    }

    public class UserWall
    {
        [Key]
        public int WId { get; set; }
        
        public Guid UserId { get; set; }

        public string WContent { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime WTime { get; set; }

        public bool isPublic { get; set; }




        public Guid WriterId { get; set; }
        [ForeignKey("WriterId")]
        public virtual CustomProfile Writer { get; set; }

        public int? AId { get; set; }
        public virtual Attachment Attachments { get; set; }

        public int? ParentWId { get; set; }
        public virtual UserWall Parent { get; set; }

        public virtual ICollection<UserWall> Children { get; set; }
    }

    public class UserNotice
    {
        [Key]
        public int NId { get; set; }

        public string NContent { get; set; }

        public string NAbout { get; set; }

        public bool NNew { get; set; }

        public Guid? SenderId { get; set; }
        [ForeignKey("SenderId")]
        public virtual CustomProfile Sender { get; set; }

        public Guid UserId { get; set; }
        public virtual CustomProfile Profiles { get; set; }
    }

    public class Group
    {
        [Key]
        public int GId { get; set; }

        public string GName { get; set; }

        public string GDescription { get; set; }

        [DataType(DataType.ImageUrl)]
        public string Avatar { get; set; }

        [Range(0, 2)]
        public Byte GPermission { get; set; }
        /* A group access setting
         * 0 - Private. Invite only
         * 1 - Accept request - will be list on the main page
         * 2 - free to join - will be list
         */

        public Guid OwnerId { get; set; }

        [RegularExpression("[0-3][0-3][0-1]")]
        public string WallPermission { get; set; }
        /* Permission go with number here
         * 0 - None
         * 1 - Read
         * 2 - Write
         * 3 - Read/Write
         */

   
        public virtual ICollection<CustomProfile> Members { get; set; }

        public virtual ICollection<GroupWall> GroupWalls { get; set; }

        public virtual ICollection<GroupRequest> GroupRequests { get; set; }

        public Group()
        {
            Members = new HashSet<CustomProfile>();
            WallPermission = "310";
            GPermission = 1;
            Avatar = "/Content/images/group.png";
        }
    }

    public class GroupWall
    {
        [Key]
        public int GWId { get; set; }

        public string GWContent { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime GWTime { get; set; }

        public int GId { get; set; }
        public virtual Group Groups { get; set; }

        public bool isPublic { get; set; }




        public Guid WriterId { get; set; }
        [ForeignKey("WriterId")]
        public virtual CustomProfile Writer { get; set; }

        public int? AId { get; set; }
        public virtual Attachment Attachments { get; set; } 

        public int? ParentGWId { get; set; }
        public virtual GroupWall Parent { get; set; }

        public virtual ICollection<GroupWall> Children { get; set; }
    }

    public class GroupRequest
    {
        [Key]
        public int GRId { get; set; }

        public string GRHi { get; set; }

        public bool isInvite { get; set; }

        [DataType(DataType.DateTime)]
        public DateTime GRTime { get; set; }





        public Guid UserId { get; set; }
        public virtual CustomProfile Profile { get; set; }

        public int GId { get; set; }
        public virtual Group Groups { get; set; }
    }

    public class Attachment
    {
        [Key]
        public int AId { get; set; }

        [DataType(DataType.Url)]
        public string ASource { get; set; }

        [Required]
        public string AKey { get; set; }

        [Required]
        public string AName { get; set; }

        public bool isPublic { get; set; }

        public bool isImage { get; set; }




        public int? FId { get; set; }
        public virtual LiveFolder Parent { get; set; }

        public Guid UserId { get; set; }
        public virtual CustomProfile Profiles { get; set; }
    }

    public class CanvasSession
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid ID { get; set; }
        public bool readOnly { get; set; }
        public bool allowChat { get; set; }
        public bool isPublic { get; set; }
        public string Title { get; set; }

        public Guid UserId { get; set; }
        public virtual CustomProfile Profiles { get; set; }
        public CanvasSession()
        {
            Title = "No Title!";
        }
    }

    public class ESNCoreDbContext : DbContext
    {
        public DbSet<App> Apps { get; set; }
        public DbSet<CustomProfile> Profiles { get; set; }
        public DbSet<FriendRequest> FriendRequests { get; set; }
        public DbSet<UserWall> UserWalls { get; set; }
        public DbSet<UserNotice> UserNotices { get; set; }
        public DbSet<Group> Groups { get; set; }
        public DbSet<GroupWall> GroupWalls { get; set; }
        public DbSet<GroupRequest> GroupRequests { get; set; }
        public DbSet<Attachment> Attachments { get; set; }
        public DbSet<LiveFolder> Folders { get; set; }
        public DbSet<LiveOAuthResponse> AppToken { get; set; }
        public DbSet<CanvasSession> CanvasSessions { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CustomProfile>().HasMany(p => p.Friends).WithMany();
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>();
            base.OnModelCreating(modelBuilder);
        }
    }


    public class CanvasData
    {
        public UInt16 type { get; set; }
        public string data { get; set; }
    }

    public class LiveFolderStruct
    {
        public string id { get; set; }
        //public From from { get; set; }
        public string name { get; set; }
        public object description { get; set; }
        public string parent_id { get; set; }
        public string upload_location { get; set; }
        public int count { get; set; }
        public string link { get; set; }
        public string type { get; set; }
        //public SharedWith shared_with { get; set; }
        public string created_time { get; set; }
        public string updated_time { get; set; }
    }
}